package Bill.app.client;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.*;

import Bill.util.Configuration;

class NouveauCompte extends JFrame
    implements ActionListener, KeyListener
{

    NouveauCompte()
    {
        super("BZHMessenger 1.0.0");
        login = new JTextField();
        tel = new JTextField();
        ville = new JTextField();
        pass = new TextField();
        info = new JLabel();
        valider = new JButton("VALIDER");
        setIconImage((new ImageIcon("images.gif")).getImage());
        JPanel pan = new JPanel();
        pan.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("NOUVEAU COMPTE "), BorderFactory.createEmptyBorder(5, 5, 5, 5)), pan.getBorder()));
        pan.setLayout(new GridLayout(5, 2, 10, 10));
        pan.add(new JLabel("           IDENTIFIANT:"));
        pan.add(login);
        pan.add(new JLabel("           PASSWORD:"));
        pass.setEchoChar('*');
        pan.add(pass);
        pan.add(new JLabel("           TEL:"));
        pan.add(tel);
        pan.add(new JLabel("          SERVICE:"));
        pan.add(ville);
        Font font = new Font("Sanserif", 1, 15);
        login.setFont(font);
        pass.setFont(font);
        tel.setFont(font);
        ville.setFont(font);
        pan.add(info);
        pan.add(valider);
        valider.setToolTipText("envoyer les donn\351es  au serveur");
        add(pan);
        setSize(400, 400);
        Dimension dimensionecran = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((dimensionecran.width - getWidth()) / 2 - 100, (dimensionecran.height - getHeight()) / 2);
        setVisible(true);
        setResizable(false);
        ville.addKeyListener(this);
        valider.addActionListener(this);
    }

    public void keyPressed(KeyEvent e)
    {
        if(e.getKeyCode() == 10)
            valider.doClick();
    }

    public void keyReleased(KeyEvent keyevent)
    {
    }

    public void keyTyped(KeyEvent keyevent)
    {
    }

    public void actionPerformed(ActionEvent f)
    {
        if(f.getSource() == valider)
            if(login.getText().compareToIgnoreCase("") == 0 || pass.getText().compareToIgnoreCase("") == 0)
            {
                info.setForeground(Color.red);
                info.setText("login ou mot de pass incorect");
            } else
            {
                try
                {Configuration.loadConfiguration();
                    Socket socket = new Socket(Configuration.IPserveur,Configuration.Config_portServeur);
                    PrintWriter ecriture = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                    BufferedReader lecture = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    if(tel.getText().compareToIgnoreCase("") == 0)
                        tel.setText("X");
                    if(ville.getText().compareToIgnoreCase("") == 0)
                        ville.setText("X");
                    String mess = (new StringBuilder("5:")).append(login.getText()).append(":").append(pass.getText()).append(":").append(tel.getText()).append(":").append(ville.getText()).toString();
                    ecriture.println(mess);
                    mess = lecture.readLine();
                    lecture.close();
                    ecriture.close();
                    socket.close();
                    if(mess.compareToIgnoreCase("login_error") == 0)
                    {
                        info.setText("ce login existe dej\340!");
                    } else
                    {
                        JOptionPane.showMessageDialog(null, mess);
                        setVisible(false);
                    }
                }
                catch(UnknownHostException e)
                {
                    info.setForeground(Color.green);
                    info.setText("verifier votre connexion");
                }
                catch(IOException e)
                {
                    info.setForeground(Color.blue);
                    info.setText("erreur de connexion");
                }
            }
    }

    JTextField login;
    JTextField tel;
    JTextField ville;
    TextField pass;
    JLabel info;
    JButton valider;
}
